-- Function: c_order_chk_restrinctions_trg()

-- DROP FUNCTION c_order_chk_restrinctions_trg();

CREATE OR REPLACE FUNCTION c_order_chk_restrinctions_trg()
  RETURNS trigger AS
$BODY$ DECLARE 

v_n NUMERIC;
v_OrderLine_Offer_ID VARCHAR(32); --OBTG:VARCHAR2--
v_Price NUMERIC;
v_lineNo NUMERIC;
v_PriceNew NUMERIC;
v_amt NUMERIC;
v_DateNull TIMESTAMP := TO_DATE('01-01-1900','DD-MM-YYYY');

BEGIN
  IF AD_isTriggerEnabled()='N' THEN IF TG_OP = 'DELETE' THEN RETURN OLD; ELSE RETURN NEW; END IF; 
  END IF;

  IF TG_OP = 'UPDATE' THEN
    IF(OLD.Processed='Y' AND OLD.Docaction <> 'CL'
          AND(
               (COALESCE(OLD.C_BPartner_ID, '0') <> COALESCE(NEW.C_BPartner_ID, '0'))
               OR(COALESCE(OLD.DOCUMENTNO, '.') <> COALESCE(NEW.DOCUMENTNO, '.'))
               OR(COALESCE(OLD.C_DOCTYPE_ID, '0') <> COALESCE(NEW.C_DOCTYPE_ID, '0'))
               OR(COALESCE(OLD.C_DOCTYPETARGET_ID, '0') <> COALESCE(NEW.C_DOCTYPETARGET_ID, '0'))
               OR(COALESCE(OLD.DATEORDERED, v_DateNull) <> COALESCE(NEW.DATEORDERED, v_DateNull))
               OR(COALESCE(OLD.C_BPARTNER_LOCATION_ID, '0') <> COALESCE(NEW.C_BPARTNER_LOCATION_ID, '0'))
               OR(COALESCE(OLD.PAYMENTRULE, '.') <> COALESCE(NEW.PAYMENTRULE, '.'))
               OR(COALESCE(OLD.C_PAYMENTTERM_ID, '0') <> COALESCE(NEW.C_PAYMENTTERM_ID, '0'))
               OR(COALESCE(OLD.C_CHARGE_ID, '0') <> COALESCE(NEW.C_CHARGE_ID, '0'))
               OR(COALESCE(OLD.CHARGEAMT, 0) <> COALESCE(NEW.CHARGEAMT, 0))
               OR(COALESCE(OLD.GRANDTOTAL, 0) <> COALESCE(NEW.GRANDTOTAL, 0))
	       OR(COALESCE(OLD.TOTALLINES, 0) <> COALESCE(NEW.TOTALLINES, 0))
               OR(COALESCE(OLD.BILLTO_ID, '0') <> COALESCE(NEW.BILLTO_ID, '0'))
               OR(COALESCE(OLD.DELIVERYRULE, '.') <> COALESCE(NEW.DELIVERYRULE, '.'))
               OR(COALESCE(OLD.M_PRICELIST_ID, '0') <> COALESCE(NEW.M_PRICELIST_ID, '0'))
               OR(COALESCE(OLD.AD_USER_ID, '0') <> COALESCE(NEW.AD_USER_ID, '0'))
               OR(COALESCE(OLD.AD_ORGTRX_ID, '0') <> COALESCE(NEW.AD_ORGTRX_ID, '0'))
               OR(COALESCE(OLD.M_WAREHOUSE_ID, '0') <> COALESCE(NEW.M_WAREHOUSE_ID, '0'))
               OR(COALESCE(OLD.DROPSHIP_USER_ID, '0') <> COALESCE(NEW.DROPSHIP_USER_ID, '0'))
               OR(COALESCE(OLD.DROPSHIP_BPARTNER_ID, '0') <> COALESCE(NEW.DROPSHIP_BPARTNER_ID, '0'))
               OR(COALESCE(OLD.DROPSHIP_LOCATION_ID, '0') <> COALESCE(NEW.DROPSHIP_LOCATION_ID, '0'))
               OR(COALESCE(OLD.DELIVERYVIARULE, '.') <> COALESCE(NEW.DELIVERYVIARULE, '.'))
               OR(COALESCE(OLD.PRIORITYRULE,'.') <> COALESCE(NEW.PRIORITYRULE, '.'))
               OR(COALESCE(old.AD_ORG_ID, '0') <> COALESCE(new.AD_ORG_ID, '0'))
               OR(COALESCE(old.AD_CLIENT_ID, '0') <> COALESCE(new.AD_CLIENT_ID, '0'))
          )
    )THEN
      RAISE EXCEPTION '%', '@20501@' ; --OBTG:-20000--
    END IF;
    IF(OLD.Posted='Y' AND
      ((COALESCE(OLD.DATEACCT, v_DateNull) <> COALESCE(NEW.DATEACCT, v_DateNull))
      OR(COALESCE(OLD.C_CAMPAIGN_ID, '0') <> COALESCE(NEW.C_CAMPAIGN_ID, '0'))
      OR(COALESCE(OLD.C_PROJECT_ID, '0') <> COALESCE(NEW.C_PROJECT_ID, '0'))
      OR(COALESCE(OLD.C_ACTIVITY_ID, '0') <> COALESCE(NEW.C_ACTIVITY_ID, '0'))
      OR(COALESCE(OLD.USER1_ID, '0') <> COALESCE(NEW.USER1_ID, '0'))
      OR(COALESCE(OLD.USER2_ID, '0') <> COALESCE(NEW.USER2_ID, '0'))
      OR(COALESCE(OLD.A_ASSET_ID, '0') <> COALESCE(NEW.A_ASSET_ID, '0')))) THEN
      RAISE EXCEPTION '%', '@20501@' ; --OBTG:-20000--
    END IF;
 /*  IF (COALESCE(OLD.C_BPartner_ID, '0')!=COALESCE(NEW.C_BPartner_ID, '0')) OR (COALESCE(OLD.M_PriceList_ID,'0') != COALESCE(NEW.M_PriceList_ID,'0'))  THEN
      SELECT COUNT(*)
        INTO v_n
        FROM C_ORDERLINE
       WHERE C_Order_ID = NEW.C_Order_ID;

       IF v_n>0 THEN
         RAISE EXCEPTION '%', '@20502@' ; --OBTG:-20000--
       END IF;
     END IF;*/
  END IF;

  IF(TG_OP = 'DELETE') THEN
    IF OLD.Processed='Y' THEN
      RAISE EXCEPTION '%', '@20501@' ; --OBTG:-20000--
    END IF;
  END IF;
  IF(TG_OP = 'INSERT') THEN
    IF NEW.Processed='Y' THEN
      RAISE EXCEPTION '%', '@20501@' ; --OBTG:-20000--
    END IF;
  END IF;
IF TG_OP = 'DELETE' THEN RETURN OLD; ELSE RETURN NEW; END IF; 
END 

; $BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION c_order_chk_restrinctions_trg()
  OWNER TO tad;
